package com.wsoft.im.controller;

import com.wsoft.im.query.group.member.ToggleLeaderQuery;
import com.wsoft.im.query.group.member.UpdateGroupMemberQuery;
import com.wsoft.im.query.group.member.UpdateMemberQuery;
import com.wsoft.im.service.IImGroupMemberService;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 *  前端控制器
 * @author Z001
 * @since 2025年01月14日 09:21:20
 */
@RestController
@Tag(name = "群成员")
@RequestMapping("/im/member")
public class ImGroupMemberController {

    @Resource
    private IImGroupMemberService memberService;

    @Operation(summary = "新增群成员", description = "新增群成员")
    @PostMapping("/addMember")
    public ResponseResult addMember(@RequestBody @Valid UpdateMemberQuery query) {
        boolean b = memberService.addMember(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "移除群成员", description = "移除群成员")
    @PostMapping("/removeMember")
    public ResponseResult removeMember(@RequestBody @Valid UpdateMemberQuery query) {
        boolean b = memberService.removeMember(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "群成员退群", description = "群成员退群")
    @PostMapping("/quitGroup")
    public ResponseResult quitGroup(@RequestBody @Valid UpdateGroupMemberQuery query) {
        boolean b = memberService.quitGroup(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "解散群", description = "解散群")
    @PostMapping("/dissolveGroup")
    public ResponseResult dissolveGroup(@RequestBody @Valid UpdateGroupMemberQuery query) {
        boolean b = memberService.dissolveGroup(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "获取群成员信息", description = "获取群成员信息")
    @GetMapping("/getMemberList")
    public ResponseResult getMemberList(@RequestParam("groupId") Long groupId) {
        return ResponseResult.success(memberService.getGroupMember(groupId));
    }

    @Operation(summary = "获取当前用户在群组内的信息", description = "获取当前用户在群组内的信息")
    @GetMapping("/getInfo")
    public ResponseResult getInfo(@RequestParam("groupId") Long groupId) {
        return ResponseResult.success(memberService.getInfo(groupId));
    }

    @Operation(summary = "切换群主", description = "切换群主")
    @PostMapping("/toggleLeader")
    public ResponseResult toggleLeader(@RequestBody @Valid ToggleLeaderQuery query) {
        boolean b = memberService.toggleLeader(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

}

